import gtk
from symbide import ModuleManager

class MatrixPreview(gtk.TreeView):
    def __init__(self):
        gtk.TreeView.__init__(self)
        
        if ModuleManager.hasModule('numpy'):
            from numpy import shape
            self.shape = shape
        else:
            self.shape = lambda x:(None, max(x))
        
        self.renderer = gtk.CellRendererText()
        self.renderer.set_property('cell-background-gdk', gtk.gdk.color_parse('#EEEEEE'))
        self.set_headers_visible(False)
        self.show()
        
    def preview(self, data):
        for col in self.get_columns():
            self.remove_column(col)
        
        columns = [str]*self.shape(data)[1]
        for t in [str, float, int, type(None)]:
            if ModuleManager.hasModule('numpy') and data.dtype==t:
                columns = [t]*self.shape(data)[1]
                break
            
        for i in range(len(columns)):
            self.append_column(gtk.TreeViewColumn(None, self.renderer, text=i))
        
        self.model = gtk.ListStore(*columns)
        i=0
        
        for row in data:
            self.model.append(row.tolist()[0])
            
        self.set_model(self.model)
        

        